package com.ruoyi.platform.system.user.pojo.role;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.platform.system.role.pojo.RoleDO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户和角色关联表
 *
 * @author lcf
 * @since 2020-11-30 05:47:54
 */
@ApiModel
@Data
@NoArgsConstructor
public class UserRoleDTO {
    /**
     * 角色ID
     */
    @ApiModelProperty("角色ID")
    private Long roleId;
    /**
     * 用户ID
     */
    @ApiModelProperty("用户ID")
    private Long userId;

    /**
     * 角色名称
     */
    @ApiModelProperty("角色名称")
    private String roleName;

    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    public UserRoleDTO(UserRoleDO info, List<RoleDO> roleList) {
        if (info != null) {
            this.roleId = info.getRoleId();
            this.userId = info.getUserId();
            if (CollectionUtils.isNotEmpty(roleList)) {
                roleList.stream().filter(x -> Objects.equals(info.getRoleId(), x.getRoleId()))
                        .findAny().ifPresent(role -> {
                    this.roleName = role.getRoleName();
                    this.createTime = role.getCreateTime();
                });
            }
        }
    }
}